package com.jcl.recruit.resume.service;

import com.jcl.recruit.core.constants.ResumeLogType;
import com.jcl.recruit.core.web.utils.RequestUtils;
import com.jcl.recruit.log.logic.LoggerProxy;
import com.jcl.recruit.resume.logic.ResumeBasicInfoLogic;
import com.jcl.recruit.resume.logic.ResumeEvaluateLogic;
import com.jcl.recruit.resume.logic.ResumeTransferLogic;
import com.jcl.recruit.resume.model.ResumeBasicInfo;
import com.jcl.recruit.resume.model.ResumeEvaluate;
import com.jcl.recruit.server.elastic.SearchResumeServer;
import com.jcl.recruit.server.elastic.vo.SearchResumeBasicEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName ResumeOperateService
 * @Description TODO
 * @Author yyliu
 * @Date 2018/9/17 11:03
 * @Version 1.0
 **/
@Service
public class ResumeEvaluateService {

    @Autowired
    private ResumeBasicInfoLogic resumeBasicInfoLogic;

    @Autowired
    private SearchResumeServer searchResumeServer;

    @Autowired
    private ResumeEvaluateLogic resumeEvaluateLogic;

    @Autowired
    private ResumeTransferLogic resumeTransferLogic;

    @Autowired
    private LoggerProxy loggerProxy;

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 12:19 2018/9/17
     * @Param [resumeId]
     * @return java.util.List<com.example.model.ResumeEvaluate>
     **/
    public List<ResumeEvaluate> getSearchDetailList(Integer resumeId) {
        List<ResumeEvaluate> list = new ArrayList<>();
        String corpCode = RequestUtils.getCorpCode();
        ResumeBasicInfo resume = resumeBasicInfoLogic.findOneByExternalKey(corpCode, resumeId.toString());
        if (null != resume) {
            List<ResumeEvaluate> evaluateList = resumeEvaluateLogic.findList(corpCode, resume.getId());
            list = resumeEvaluateLogic.contractDetailList(corpCode, evaluateList);
        }
        return list;
    }

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 12:19 2018/9/17
     * @Param [resumeId]
     * @return java.util.List<com.example.model.ResumeEvaluate>
     **/
    public List<ResumeEvaluate> getDetailList(Integer resumeId) {

        String corpCode = RequestUtils.getCorpCode();
        List<ResumeEvaluate> evaluateList = resumeEvaluateLogic.findList(corpCode, resumeId);

        return resumeEvaluateLogic.contractDetailList(corpCode, evaluateList);
    }

    /* *
     * @Author yyliu
     * @Description 添加简历评价
     * @Date 11:07 2018/9/17
     * @Param [evaluate]
     * @return int
     **/
    public int addResumeEvaluate(ResumeEvaluate evaluate, int fromType) {

        String corpCode = RequestUtils.getCorpCode();
        String userId = RequestUtils.getUserId();
        Integer resumeId = evaluate.getResumeId();

        ResumeBasicInfo resume = null;
        if (0 == fromType) {
            resume = resumeBasicInfoLogic.findOneByExternalKey(corpCode, resumeId.toString());
        } else {
            resume = resumeBasicInfoLogic.findOne(resumeId);
        }
        if (null == resume) {
            try {
                resume = resumeTransferLogic.transferResumeById(resumeId,
                        ResumeLogType.TYPE_ADD_EVALUATE, corpCode);
                // 添加日志
                loggerProxy.addResumeLog(resume.getId(), ResumeLogType.TYPE_ADD_EVALUATE);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (null != resume) {
            resumeId = resume.getId();
        }

        evaluate.setCorpCode(corpCode);
        evaluate.setAddUser(Integer.parseInt(userId));
        evaluate.setCreateTime(new Date());
        evaluate.setResumeId(resumeId);
        resumeEvaluateLogic.add(evaluate);
        // 添加日志
        loggerProxy.addResumeLog(resumeId, ResumeLogType.TYPE_APPLY_EVALUATE);
        return 1;
    }
}
